# set working directory
setwd('~/replication_files')

# function to load and install packages
load_packages<-function(packages){
  sapply(packages,function(x){
    f<-require(x,character.only=T)
    if(!f) install.packages(x); f<-require(x,character.only=T)
    return(f)},USE.NAMES=T)
}

# load packages
load_packages(c('data.table'))

(TabDT<-readRDS('Table2.rds'))

# top of Latex table
tab_head<-c('\\begin{table}[ht]',
            '\\caption{Top domains for vaccine content}',
            '\\begin{center}',
            '\\begin{threeparttable}',
            '\\begin{tabular}{lrlr}',
            '\\toprule',
            '\\multicolumn{2}{c}{\\myuline{Non-skeptical webpages}} & \\multicolumn{2}{c}{\\myuline{Vaccine-skeptical webpages}} \\\\',
            '\\multicolumn{1}{c}{Domain} & \\multicolumn{1}{c}{$N$} & \\multicolumn{1}{c}{Domain} & \\multicolumn{1}{c}{$N$} \\\\',
            '\\midrule')

# collapse columns together separated by &
(tab_mid<-apply(TabDT,1,paste,collapse=' & '))
# add line return at end of line
(tab_mid<-paste(tab_mid,'\\\\'))

# bottom of Latex table
tab_foot<-c('\\bottomrule',
            '\\end{tabular}',
            '\\end{threeparttable}',
            '\\end{center}',
            '\\label{table:tab2}',
            '\\end{table}')

# create final table by adding the parts together in a string
(tab_fin<-c(tab_head,tab_mid,tab_foot))

# write to file
writeLines(tab_fin,'Table2.tex')
